#include "gdt.h"
#include "tss.h"

/*
 * Definicion de la GDT
 */

gdt_entry gdt[GDT_COUNT] = {

	/* 0x00 Descriptor nulo */

	(gdt_entry){ 
		(unsigned short) 0x0000, 
		(unsigned short) 0x0000,
		(unsigned char) 0x00, 
		(unsigned char) 0x0, 
		(unsigned char) 0, 
		(unsigned char) 0, 
		(unsigned char) 0, 
		(unsigned char) 0x0000,
		(unsigned char) 0,  
		(unsigned char) 0,  
		(unsigned char) 0,  
		(unsigned char) 0, 
		(unsigned char) 0x00 
	},

	/* 0x08 Descriptor de Codigo Kernel */

	(gdt_entry){ 
		(unsigned short) 0xFFFF, 
		(unsigned short) 0x0000,
		(unsigned char) 0x00, 
		(unsigned char) 9, 		// 1001b = Codigo - ¬ ExpandDown - readEnable - Accessed
		(unsigned char) 1, 		// Es de datos o codigo
		(unsigned char) 0, 		// Ring 0
		(unsigned char) 1, 		// Segment present
		(unsigned char) 0xF,
		(unsigned char) 0,  	// Siempre en 0 (reservado)
		(unsigned char) 0,   	// Siempre en 0 (reservado)
		(unsigned char) 1,  	// Byte = 32 bit mode (en 0 es 16bit mode)
		(unsigned char) 0, 		// granularity: byte
		(unsigned char) 0x00 
	},

	/* 0x10 Descriptor de Datos Kernel */

	(gdt_entry){ 
		(unsigned short) 0xFFFF, 
		(unsigned short) 0x0000,
		(unsigned char) 0x00, 
		(unsigned char) 3,	 	// 0011b = Datos - NonConforming - ¬ writeEnable - Accessed
		(unsigned char) 1, 		// Es de datos o codigo
		(unsigned char) 0, 		// Ring 0
		(unsigned char) 1, 		// Segment present
		(unsigned char) 0xF,
		(unsigned char) 0,  	// Siempre en 0 (reservado)
		(unsigned char) 0,   	// Siempre en 0 (reservado)
		(unsigned char) 1,  	// Byte = 32 bit
		(unsigned char) 0, 		// granularity: byte
		(unsigned char) 0x00 
	},

	/* 0x18 Descriptor de datos de la memoria de video (ring 0) */

	(gdt_entry){ 
		(unsigned short) 0xFFFF, 
		(unsigned short) 0x8000,
		(unsigned char) 0x0B, 
		(unsigned char) 0x2,	 	// 0011b = Datos - NonConforming - ¬ writeEnable - Accessed
		(unsigned char) 1, 		// Es de datos o codigo
		(unsigned char) 0, 		// Ring 0
		(unsigned char) 1, 		// Segment present
		(unsigned char) 0xF,
		(unsigned char) 0,  	// Siempre en 0 (reservado)
		(unsigned char) 0,   	// Siempre en 0 (reservado)
		(unsigned char) 1,  	// Byte = 32 bit
		(unsigned char) 0, 		// granularity: byte
		(unsigned char) 0x00 
	},

	/* 0x20 TSS descriptor nil */

	(gdt_entry){
		(unsigned short) 0x0067, 
		(unsigned short) 0,
		(unsigned char) 0, 
		(unsigned char) 0x9,	// No Busy
		(unsigned char) 0, 		// Es de codigo
		(unsigned char) 0, 		// Ring 0
		(unsigned char) 1, 		// Segment present
		(unsigned char) 0,
		(unsigned char) 0,  	// AVL ??
		(unsigned char) 0,   	// Siempre en 0
		(unsigned char) 0,  	// Siempre en 0
		(unsigned char) 1, 		// granularity: byte
		(unsigned char) 0
	},

	/* 0x28 TSS descriptor pintor */

	(gdt_entry){
		(unsigned short) 0x0067, 
		(unsigned short) 0,
		(unsigned char) 0, 
		(unsigned char) 0x9,	// No Busy
		(unsigned char) 0, 		// Es de codigo
		(unsigned char) 0, 		// Ring 0
		(unsigned char) 1, 		// Segment present
		(unsigned char) 0,
		(unsigned char) 0,  	// AVL ??
		(unsigned char) 0,   	// Siempre en 0
		(unsigned char) 0,  	// Siempre en 0
		(unsigned char) 1, 		// granularity: byte
		(unsigned char) 0
	},

	/* 0x30 TSS descriptor traductor */

	(gdt_entry){
		(unsigned short) 0x0067, 
		(unsigned short) 0, 
		(unsigned char) 0, 
		(unsigned char) 0x9,	// No Busy
		(unsigned char) 0, 		// Es de codigo
		(unsigned char) 0, 		// Ring 0
		(unsigned char) 1, 		// Segment present
		(unsigned char) 0,
		(unsigned char) 0,  	// AVL ??
		(unsigned char) 0,   	// Siempre en 0
		(unsigned char) 0,  	// Siempre en 0
		(unsigned char) 1, 		// granularity: byte
		(unsigned char) 0
	}

};

/*
 * Definicion del GDTR
 */

gdt_descriptor GDT_DESC = {sizeof(gdt)-1, (unsigned int)&gdt};
